#include "Metgine/UI/Widget.hpp"
#include "Metgine/UI/UIManager.hpp"
#include "Metgine/Input/MouseManager.hpp"

using namespace Metgine::Input;

namespace Metgine
{
namespace UI
{

Widget::Widget(const std::string& name, Group::Group group, bool visible, bool enabled) :
    m_name(name),
    m_group(group),
    m_visible(visible),
    m_enabled(enabled),
    m_mouseClickCallback(NULL),
    m_mouseEnterCallback(NULL),
    m_mouseLeaveCallback(NULL)
{
}

Widget::Widget(const std::string& name, Group::Group group, const Vector2i& position, const Vector2i& extents) :
    m_name(name),
    m_position(position),
    m_size(extents),
    m_group(group),
    m_mouseClickCallback(NULL),
    m_mouseEnterCallback(NULL),
    m_mouseLeaveCallback(NULL)
{
}

void Widget::SetSprite(const Sprite& sprite)
{
    m_sprite = sprite;
}

void Widget::SetOwner(UIManager* owner)
{
    m_owner = owner;
}

const std::string& Widget::GetName() const
{
    return m_name;
}

const int Widget::GetWidth() const
{
    return m_size.x;
}

const int Widget::GetHeight() const
{
    return m_size.x;
}

void Widget::SetPosition(const Vector2i& position)
{
    m_position = position;
    m_sprite.SetPosition(m_position.x, -m_position.y);
}

const Vector2i& Widget::GetPosition() const
{
    return m_position;
}

void Widget::SetSize(const Vector2i& size)
{
    m_size = size;
}

bool Widget::Contains(const sf::Vector2i& point)
{
    return ((point.x >= m_position.x && point.x <= m_position.x + m_size.x) && (point.y >= m_position.y && point.y <= m_position.y + m_size.y));
}

void Widget::Update(float elapsedTime)
{
    if (Contains(sf::Vector2i(MouseManager::Get()->MouseX(), MouseManager::Get()->MouseY())))
    {
        OnMouseEnter();
        if (MouseManager::Get()->IsAnyButtonDown())
            OnClick();
    }
    else
        OnMouseLeave();
}

void Widget::Render()
{
}

bool Widget::IsEnabled() const
{
    return m_enabled;
}

void Widget::SetEnabled(bool state)
{
    m_enabled = state;
}

bool Widget::IsVisible() const
{
    return m_visible;
}

void Widget::SetVisible(bool state)
{
    m_visible = state;
}

void Widget::SetGroup(Group::Group newGroup)
{
    m_group = newGroup;
}

unsigned int Widget::GetGroup() const
{
    return m_group;
}

bool Widget::KeyDown(sf::Key::Code key, bool shift, bool control, bool alt)
{
    return false;
}

bool Widget::KeyUp(sf::Key::Code key, bool shift, bool control, bool alt)
{
    return false;
}

void Widget::OnClick()
{
    if (m_mouseClickCallback)
        m_mouseClickCallback(this, MouseManager::Get()->GetLastPressed());
}

void Widget::OnMouseEnter()
{
    if (m_mouseEnterCallback)
        m_mouseEnterCallback(this);
}

void Widget::OnMouseLeave()
{
    if (m_mouseLeaveCallback)
        m_mouseLeaveCallback(this);
}

void Widget::SetOnClick(MouseClickCallback cb)
{
    m_mouseClickCallback = cb;
}

void Widget::SetOnMouseEnter(MouseEnterCallback cb)
{
    m_mouseEnterCallback = cb;
}

void Widget::SetOnMouseLeave(MouseLeaveCallback cb)
{
    m_mouseLeaveCallback = cb;
}

}; // UI
}; // Metgine
